package com.cloudspace.jindun.activity.Woyao;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.misc.Utils;
import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.constants.Constants;
import com.cloudspace.jindun.network.url.YXHttpUrl;
import com.cloudspace.jindun.update.UpdateAgent;
import com.cloudspace.jindun.user.UserSessionManager;
import com.cloudspace.jindun.utils.AppPreferences;
import com.cloudspace.jindun.utils.Convert;
import com.cloudspace.jindun.utils.FileUtil;
import com.cloudspace.jindun.utils.MMAlert;
import com.cloudspace.jindun.utils.TaskUtil;
import com.cloudspace.jindun.utils.ToastMaker;
import com.cloudspace.jindun.utils.Utility;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.cloudspace.jindun.R.id.sys_swi02;


public class SystemSettingsActivity extends CustomTitleBaseActivity {


    @Bind(R.id.p1_t1)
    TextView mP1T1;
    @Bind(R.id.p1_sc1)
    SwitchCompat mP1Sc1;
    @Bind(R.id.huitiao01)
    TextView mHuitiao01;
    @Bind(R.id.p1_t2)
    TextView mP1T2;
    @Bind(sys_swi02)
    SwitchCompat mSysSwi02;
    @Bind(R.id.part1)
    RelativeLayout mPart1;
    @Bind(R.id.p2_title)
    TextView mP2Title;
    @Bind(R.id.p2_line)
    TextView mP2Line;
    @Bind(R.id.p2_t1)
    TextView mP2T1;
    @Bind(R.id.p2_line2)
    TextView mP2Line2;
    @Bind(R.id.p2_t2)
    TextView mP2T2;
    @Bind(R.id.p2_line3)
    TextView mP2Line3;
    @Bind(R.id.p2_t3)
    TextView mP2T3;
    @Bind(R.id.sys_cbox_tx_2g)
    CheckBox mSysCboxTx2g;
    @Bind(R.id.sys_cbox_tx_4g)
    CheckBox mSysCboxTx4g;
    @Bind(R.id.sys_cbox_tx_weifig)
    CheckBox mSysCboxTxWeifig;
    @Bind(R.id.part2)
    RelativeLayout mPart2;
    @Bind(R.id.p3_title)
    TextView mP3Title;
    @Bind(R.id.p3_line)
    TextView mP3Line;
    @Bind(R.id.p3_t1)
    TextView mP3T1;
    @Bind(R.id.p3_line2)
    TextView mP3Line2;
    @Bind(R.id.p3_t2)
    TextView mP3T2;
    @Bind(R.id.p3_line3)
    TextView mP3Line3;
    @Bind(R.id.p3_t3)
    TextView mP3T3;
    @Bind(R.id.sys_cbox_yy_2g)
    CheckBox mSysCboxYy2g;
    @Bind(R.id.sys_cbox_yy_4g)
    CheckBox mSysCboxYy4g;
    @Bind(R.id.sys_cbox_yy_weifig)
    CheckBox mSysCboxYyWeifig;
    @Bind(R.id.part3)
    RelativeLayout mPart3;
    @Bind(R.id.sys_qlhc_tv)
    TextView mSysQlhcTv;
    @Bind(R.id.sys_qlhc)
    RelativeLayout mSysQlhc;
    @Bind(R.id.sys_jcbb_tv)
    TextView mSysJcbbTv;
    @Bind(R.id.sys_jcbb)
    RelativeLayout mSysJcbb;
    @Bind(R.id.sys_gywm_tv)
    TextView mSysGywmTv;
    @Bind(R.id.sys_gywm)
    RelativeLayout mSysGywm;
    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.sys_bt_logout)
    Button mSysBtLogout;
    @Bind(R.id.sys_qlhc_size)
    TextView mSysQlhcSize;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_settings, false);
        ButterKnife.bind(this);

        initView();
    }


    private void initView(){
        mP1Sc1.setChecked(AppPreferences.getBoolean(Constants.VIBRATE_SWITCH, false));
        mP1Sc1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                AppPreferences.putBoolean(Constants.VIBRATE_SWITCH, isChecked);
                mP1Sc1.setChecked(isChecked);
            }
        });

        mSysSwi02.setChecked(AppPreferences.getBoolean(Constants.INTERPHONE_SWITCH, true));
        mSysSwi02.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                AppPreferences.putBoolean(Constants.INTERPHONE_SWITCH, isChecked);
                mSysSwi02.setChecked(isChecked);
            }
        });

        mSysCboxTx2g.setChecked(AppPreferences.getBoolean(Constants.PHONE_UPDATE_2G_SWITCH, true));
        mSysCboxTx2g.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                AppPreferences.putBoolean(Constants.PHONE_UPDATE_2G_SWITCH, isChecked);
                mSysCboxTx2g.setChecked(isChecked);
            }
        });

        mSysCboxTx4g.setChecked(AppPreferences.getBoolean(Constants.PHONE_UPDATE_4G_SWITCH, true));
        mSysCboxTx4g.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                AppPreferences.putBoolean(Constants.PHONE_UPDATE_4G_SWITCH, isChecked);
                mSysCboxTx2g.setChecked(isChecked);
            }
        });

        mSysCboxTxWeifig.setChecked(AppPreferences.getBoolean(Constants.PHONE_UPDATE_WIFI_SWITCH, true));
        mSysCboxTxWeifig.setEnabled(false);
//        mSysCboxTxWeifig.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                AppPreferences.putBoolean(Constants.PHONE_UPDATE_WIFI_SWITCH, isChecked);
//                mSysCboxTxWeifig.setChecked(isChecked);
//            }
//        });

        mSysCboxYy2g.setChecked(AppPreferences.getBoolean(Constants.YY_2G_SWITCH, true));
        mSysCboxYy2g.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                AppPreferences.putBoolean(Constants.YY_2G_SWITCH, isChecked);
                mSysCboxYy2g.setChecked(isChecked);
            }
        });

        mSysCboxYy4g.setChecked(AppPreferences.getBoolean(Constants.YY_4G_SWITCH, true));
        mSysCboxYy4g.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                AppPreferences.putBoolean(Constants.YY_4G_SWITCH, isChecked);
                mSysCboxYy4g.setChecked(isChecked);
            }
        });

        mSysCboxYyWeifig.setChecked(AppPreferences.getBoolean(Constants.YY_WIFI_SWITCH, true));
//        mSysCboxYyWeifig.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                AppPreferences.putBoolean(Constants.YY_WIFI_SWITCH, isChecked);
//                mSysCboxYyWeifig.setChecked(isChecked);
//            }
//        });
        mSysCboxYyWeifig.setEnabled(false);

        mSysJcbb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new UpdateAgent().update(SystemSettingsActivity.this);
            }
        });

        mSysQlhc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showClearConfirmDialog();
            }
        });

        mSysGywm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ++aboutClickedTime;

                if (aboutClickedTime >= 2){
                    ToastMaker.showToastShort(R.string.develop_mode);
                    Configs.setServerIp(true);
                    YXHttpUrl.setHttpUri(true);
                }
            }
        });

        initCacheLayout();
    }

    private int aboutClickedTime = 0;

    private void showClearConfirmDialog() {
        MMAlert.showAlert(this,
                JindunApp.getInstance().getString(R.string.personal_set_cache_clear_confirm),
                "",
                JindunApp.getInstance().getString(R.string.ok),
                JindunApp.getInstance().getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        clearCache();
                    }
                },
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
    }

    private List<File> mCacheFiles = new ArrayList<File>();

    private void clearCacheFiles() {
        for (File file : mCacheFiles) {
            if (file != null && file.isDirectory()) {
                FileUtil.deleteQuietly(file);
            }
        }
    }

    private void initCacheLayout() {
        TaskUtil.executeSingleTask(new Runnable() {
            @Override
            public void run() {
                if (!isFinishing()) {
                    final String size = collectCacheDir();
                    updateCacheSize(size + "");
                }
            }
        });
    }

    private String collectCacheDir() {
        long size = 0;

        mCacheFiles.add(Utility.getInstallPackageDir());

        mCacheFiles.add(Utils.getDiskCacheDir(JindunApp.getInstance(), "volley"));
        mCacheFiles.add(Utils.getDiskCacheDir(JindunApp.getInstance(), "images"));

        mCacheFiles.add(Utility.getImageCacheDir());
        mCacheFiles.add(Utility.getPhotoTempDir());

        mCacheFiles.add(new File(FileUtil.getLocalFileCachePath()));
        mCacheFiles.add(new File(FileUtil.getLocalLogPath()));

        for (File file : mCacheFiles) {
            if (file == null || !file.exists()) continue;
            size += FileUtil.sizeOfDirectory(file);
        }

        return Convert.size2String(size);
    }

    private void clearCache() {
        ToastMaker.showToastShort(getString(R.string.personal_set_cache_operating));
        TaskUtil.executeSingleTask(new Runnable() {
            @Override
            public void run() {
                clearCacheFiles();
                updateCacheSize("0.0 B");
                if (!isFinishing()) {
                    ToastMaker.showToastShort(getString(R.string.personal_set_cache_completed));
                }
            }
        });
    }

    private void updateCacheSize(final String s) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mSysQlhcSize.setText(s);
            }
        });

    }


    @OnClick({R.id.back, R.id.sys_bt_logout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sys_bt_logout:
                UserSessionManager.getInstance().loginOut();
                finish();
                break;
        }
    }
}
